package org.szy.plugin.sys.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.util.UpdateEntity;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NoRepeatSubmit;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Validated;
import org.szy.core.common.controller.BaseController;
import org.szy.core.common.entity.R;
import org.szy.core.common.param.CommonStatusParam;
import org.szy.plugin.sys.entity.SysPerson;
import org.szy.plugin.sys.mapper.SysPersonMapper;
import org.szy.plugin.sys.param.SysPersonQueryParam;
import org.szy.plugin.sys.service.SysPersonService;

/**
 * 系统人员控制器
 *
 * @author wangbuhao
 * @since 2023-06-20 11:54:51
 */
@Controller
@Mapping("/sys/person")
public class SysPersonController extends BaseController<SysPerson> {

    private final String PATH_PREFIX = "sys/person/";
    @Inject
    private SysPersonService sysPersonService;
    @Inject
    private SysPersonMapper sysPersonMapper;

    /**
     * 系统人员列表页面
     */
    @Get
    @Mapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "index");
        return view;
    }

    /**
     * 系统人员编辑页
     */
    @Get
    @Mapping("/edit")
    public ModelAndView edit() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "edit");
        return view;
    }

    /**
     * 系统人员详情页
     */
    @Get
    @Mapping("/detail")
    public ModelAndView detail() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "detail");
        return view;
    }

    /**
     * 选择
     * @return
     */
    @Get
    @Mapping("/sel")
    public ModelAndView sel() {
        return new ModelAndView(PATH_PREFIX + "sel");
    }

    /**
     * 获取系统人员分页数据
     *
     * @author wangbuhao
     * @since 2023-06-20 11:54:51
     */
    @Get
    @Mapping("/page")
    @SaCheckPermission(value = "sys_person_list", mode = SaMode.OR, orRole = "wangbuhao")
    public R<Page<SysPerson>> page(SysPersonQueryParam param) {
        return R.data(sysPersonService.page(getPage(), param));
    }


    /**
     * 获取系统人员详情
     *
     * @author wangbuhao
     * @since 2023-06-20 11:54:51
     */
    @Get
    @Mapping("/getInfo")
    @SaCheckPermission(value = "sys_person_detail", mode = SaMode.OR, orRole = "wangbuhao")
    public R<SysPerson> getInfo(@NotNull Long id) {
        return R.asNull(sysPersonService.getById(id));
    }

    /**
     * 添加系统人员
     *
     * @author wangbuhao
     * @since 2023-06-20 11:54:51
     */
    @Post
    @Mapping("/add")
    @NoRepeatSubmit
    @SaCheckPermission(value = "sys_person_add", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> add(@Validated SysPerson entity) {
        boolean save = sysPersonService.add(entity);
        return save ? R.ok("保存成功", entity.getId()) : R.fail("保存失败");
    }

    /**
     * 编辑系统人员
     *
     * @author wangbuhao
     * @since 2023-06-20 11:54:51
     */
    @Put
    @Mapping("/update")
    @NoRepeatSubmit
    @SaCheckPermission(value = "sys_person_update", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> update(@Validated SysPerson entity) {
        boolean update = sysPersonService.updateById(entity);
        return R.bool(update);
    }

    /**
     * 删除系统人员
     *
     * @author wangbuhao
     * @since 2023-06-20 11:54:51
     */
    @Delete
    @Mapping("/delete")
    @SaCheckPermission(value = "sys_person_delete", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> delete(@NotNull Long id) {
        boolean delete = sysPersonService.removeById(id);
        return R.bool(delete, "删除成功", "删除失败，数据可能正在使用或已删除");
    }

    /**
     * 修改系统人员状态
     *
     * @param param 公共状态参数
     * @return 修改结果
     */
    @Put
    @Mapping("/modifyStatus")
    public R<?> modifyStatus(CommonStatusParam param) {
        boolean status = sysPersonService.modifyStatus(param);
        return R.bool(status);
    }

}
